layui.use(['table', 'admin', 'ax', 'func','laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var laydate = layui.laydate;


    var lastday = 3;

    //当前月份
    var thisdate = new Date();
    if(thisdate.getDate()<lastday){
        thisdate.setMonth(thisdate.getMonth()-1);
    }
    let monthNum = thisdate.getMonth() + 1;
    const month = thisdate.getFullYear() + "-" + (monthNum < 10 ? "0" + monthNum : monthNum);

    //渲染时间选择框
    laydate.render({
        elem: "#month",
        value: month,
        type: 'month',
        tiger: 'click'
    });

    /**
     * 出勤_督导审核店长表管理
     */
    var AttendAudit = {
        tableId: "attendAuditTable"
    };

    /**
     * 初始化表格的列
     */
    AttendAudit.initColumn = function () {
        return [[
            // {type: 'checkbox'},
            {field: 'id',hide: true, sort: false, title: 'id(主键)'},
            {field: 'submitMonth',hide: true, sort: false, title: '提交月份',width:60},
            {field: 'attendMonth', sort: false, title: '考勤月份',width: 90, fixed: true, style: 'text-align: left'},
            {field: 'dm', sort: false, title: '店名',width: 130, fixed: true, style: 'text-align: left'},
            {field: 'managerName', sort: false, title: '店长名字',width: 90, fixed: true, style: 'text-align: left'},
            {field: 'managerPhone', sort: false, title: '店长联系方式',width:120},
            {field: 'managerId',hide: true, sort: false, title: '店长id'},
            {field: 'ddSupervisorName', sort: false, title: '督导名字',width: 90},
            {field: 'ddSupervisorPhone', sort: false, title: '督导联系方式',width:120},
            {field: 'ddSupervisorId',hide: false, sort: true, title: '督导id',width:100},
            {field: 'auditStatus', sort: false,  templet: '#auditStatusTmp',title: '状态',width:100},
            {field: 'btyAreaBh', sort: false, title: '编号',width:120},
            {field: 'btyAreaId', sort: false, title: '店id',width:90},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    AttendAudit.search = function () {
        var queryData = {};
        var attendMonth= $("#month").val();
        if(attendMonth){
            queryData['attendMonth'] = attendMonth.replace("-","");
        }else{
            //当前月份
            var thisdate = new Date();
            if(thisdate.getDate()<lastday){
                thisdate.setMonth(thisdate.getMonth()-1);
            }
            let monthNum = thisdate.getMonth() + 1;
            const month = thisdate.getFullYear() + "-" + (monthNum < 10 ? "0" + monthNum : monthNum);
            queryData['attendMonth'] = month.replace("-","");
            $("#month").val(month);
        }

        queryData['dm'] = $("#dm").val();
        queryData['managerName'] = $("#managerName").val();
        queryData['managerPhone'] = $("#managerPhone").val();
        queryData['ddSupervisorName'] = $("#ddSupervisorName").val();
        queryData['ddSupervisorPhone'] = $("#ddSupervisorPhone").val();
        queryData['auditStatus'] = $("#auditStatus").val();

        table.reload(AttendAudit.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 跳转到添加页面
     */
    AttendAudit.jumpAddPage = function () {
        window.location.href = Feng.ctxPath + '/attendAudit/add'
    };

    /**
    * 跳转到编辑页面
    *
    * @param data 点击按钮时候的行数据
    */
    AttendAudit.jumpEditPage = function (data) {
        window.location.href = Feng.ctxPath + '/attendAudit/edit?=' + data.id
    };

    /**
     * 导出excel按钮
     */
    AttendAudit.exportExcel = function () {
        var checkRows = table.checkStatus(AttendAudit.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    AttendAudit.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/attendAudit/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(AttendAudit.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + AttendAudit.tableId,
        url: Feng.ctxPath + '/attendAudit/list',
        page: true,
        limit:20,
        height: "full-94",
        cellMinWidth: 100,
        cols: AttendAudit.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        AttendAudit.search();
    });
    // 重置按钮事件
    $('#btnReset').click(function () {
        $('#dm').val('');
        $('#managerName').val('');
        $('#managerPhone').val('');
        $('#ddSupervisorName').val('');
        $("#ddSupervisorPhone").val('');
        $("#auditStatus").val('');

        $("#month").val(month);
        AttendAudit.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {

    AttendAudit.jumpAddPage();

    });

    // 导出excel
    $('#btnExp').click(function () {
        AttendAudit.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + AttendAudit.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            AttendAudit.jumpEditPage(data);
        } else if (layEvent === 'delete') {
            AttendAudit.onDeleteItem(data);
        }
    });
    //初始化页面
    $(function () {
        //var attendMonth = $("#month").val();
        //AttendAudit.search();
        setTimeout(() =>{
            AttendAudit.search();
        },100);

    });
});
